﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.Core.DataAccess;

namespace WebSys.UI.Control.ZhengShou
{
    public class ProjectListControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        //public static string userid = HttpContext.Current.Session["UserID"].ToString();
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack) {
                Bind();
            }
            Button btnsearch = (Button)FindControl("btnsearch");
            Button btnadd = (Button)FindControl("btnadd");
            btnsearch.Click += new EventHandler(Go_Search);
            btnadd.Click += new EventHandler(Btn_Add_Click);
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.OnLoadData += new EventHandler(GridList_OnLoadData);
            GridList.PreRender += new EventHandler(GridList_PreRender);
            foreach (GridViewRow gvr in GridList.Rows)
            {
                Button btn = (Button)gvr.FindControl("btn_tj");
                btn.Click += new EventHandler(ChangeStatus);
                //Button btn1 = (Button)gvr.FindControl("btn_shtg");
                //btn1.Click += new EventHandler(ChangeStatus1);

                Button btnCs = (Button)gvr.FindControl("btnCs");
                btnCs.Click += new EventHandler(ChangeStatusSh);
                Button btnCh = (Button)gvr.FindControl("btnCh");
                btnCh.Click += new EventHandler(ChangeStatusSh);
                Button btnFs = (Button)gvr.FindControl("btnFs");
                btnFs.Click += new EventHandler(ChangeStatusSh);
                Button btnHs = (Button)gvr.FindControl("btnHs");
                btnHs.Click += new EventHandler(ChangeStatusSh);

                Button btn2 = (Button)gvr.FindControl("btn_tj2");
                btn2.Click += new EventHandler(ChangeStatus2);
                Button btn_txkh = (Button)gvr.FindControl("btn_txkh");
                btn_txkh.Click += new EventHandler(ChangeStatus3);
                LinkButton del = (LinkButton)gvr.FindControl("del");
                del.Click += new EventHandler(Delete_Xm_Click);
            }
            string jsstrIP = base.Page.Request.ServerVariables["HTTP_HOST"];

            //判断是否为管理员，如果是则功能均可见，但不一定能操作
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
            {
                //判断是否是内网用户，如果是代表是征收办人员
                if (jsstrIP == System.Configuration.ConfigurationManager.AppSettings["FCSERVER"])
                {
                    GridList.Columns[9].Visible = true;//提交
                    GridList.Columns[10].Visible = true;//审核
                    GridList.Columns[11].Visible = true;//撤销
                }
                else
                {
                    GridList.Columns[10].Visible = false;//审核(其他网段用户不能审核)
                }

                //如果是非市征收角色的用户
                //if (!_EluZSDataProvider.IsSZSB(userid))
                if (_EluZSDataProvider.IsSZSB(userid))
                {
                    GridList.Columns[9].Visible = false;
                    GridList.Columns[10].Visible = false;
                    GridList.Columns[11].Visible = false;
                }
            }
            //if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            //{
            //    GridList.Columns[10].Visible = false;
            //    GridList.Columns[11].Visible = false;
            //}
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Btn_Add_Click(object sender, EventArgs e)
        {
            string url = "EditProject.aspx?action=insert&query=project";
            HttpContext.Current.Response.Redirect(url);
        }
        protected void Bind()
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            GridList.PreRender += new EventHandler(GridList_PreRender);
            GridList.RowCreated += new GridViewRowEventHandler(GridList_RowCreated);
            GridList.RowDataBound += new GridViewRowEventHandler(GridList_RowDataBound);
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                List<EluZSProvider.Project> project = GetProject(GetSqlWhere());
                GridList.DataSource = project;
                GridList.RecordCount = project.Count;
                GridList.PageSize = 10;
                GridList.DataKeyNames = new string[] { "ID", "提交状态", "审批状态", "提醒开户状态" };
                GridList.DataBind();
            }
            else
            {
                List<EluZSProvider.Project> project = GetProjects(GetSqlWhere());
                GridList.DataSource = project;
                GridList.RecordCount = project.Count;
                GridList.PageSize = 10;
                GridList.DataKeyNames = new string[] { "ID", "提交状态", "审批状态", "提醒开户状态" };
                GridList.DataBind();
            }
           
        }
        protected void GridList_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void GridList_PreRender(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["TYPE"]))
            {
                GridList.Columns[7].Visible = true;
                GridList.Columns[8].Visible = false;
                GridList.Columns[9].Visible = false;
                GridList.Columns[10].Visible = false;
                GridList.Columns[11].Visible = false;
            }
        }
        protected void GridList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");

            foreach (GridViewRow gvr in GridList.Rows)
            {
                if (GridList.DataKeys[gvr.RowIndex].Values["提交状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
                else
                {
                    LinkButton lbt = (LinkButton)gvr.FindControl("del");
                    lbt.Enabled = false;
                    lbt.OnClientClick = "return true;";
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = true;
                }
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["type"]))
                {
                    LinkButton lbt1 = (LinkButton)gvr.FindControl("del");
                    lbt1.Visible = false;
                    Label lbl = (Label)gvr.FindControl("showedit");
                    lbl.Text = "查看";
                }
                //if (GridList.DataKeys[gvr.RowIndex].Values["审批状态"].ToString() == "1")
                //{
                //    Button btn = (Button)gvr.FindControl("btn_tj2");
                //    btn.Enabled = false;
                //}
                if (GridList.DataKeys[gvr.RowIndex].Values["提醒开户状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_txkh");
                    btn.Enabled = true;
                }
                else
                {
                    Button btn = (Button)gvr.FindControl("btn_txkh");
                    btn.Enabled = false;
                }
            }

        }
        protected void GridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btnadd = (Button)FindControl("btnadd");
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request["typeqs"]))
                {
                    GridList.Columns[7].Visible = false;
                    //GridList.Columns[8].Visible = false;
                    GridList.Columns[8].Visible = false;
                    GridList.Columns[9].Visible = false;
                    GridList.Columns[10].Visible = false;
                    GridList.Columns[11].Visible = false;
                    btnadd.Visible = false;
                }
                if (_EluZSDataProvider.IsQZSBM(userid))
                {
                    GridList.Columns[8].Visible = true;
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                EluZSProvider.Project rowview = (EluZSProvider.Project)e.Row.DataItem;
                string strTJ = rowview.提交状态.ToString();
                string strSHTG = rowview.审批状态.ToString();
                string strPID = rowview.Id.ToString();

                //Button btn1 = (Button)e.Row.FindControl("btn_shtg");
                Button btnCs = (Button)e.Row.FindControl("btnCs");
                Button btnCh = (Button)e.Row.FindControl("btnCh");
                Button btnFs = (Button)e.Row.FindControl("btnFs");
                Button btnHs = (Button)e.Row.FindControl("btnHs");

                if (strTJ == "1" && strSHTG == "0") { btnCs.Enabled = true; }
                if (strSHTG == "-3") { btnCh.Enabled = true; }
                if (strSHTG == "-2") { btnFs.Enabled = true; }
                if (strSHTG == "-1") { btnHs.Enabled = true; }

                if (!string.IsNullOrEmpty(HttpContext.Current.Request["typeqs"]))
                {
                    LinkButton del = (LinkButton)e.Row.FindControl("del");
                    del.Visible = false;
                    Label showedit = (Label)e.Row.FindControl("showedit");
                    showedit.Text = "查看";
                    e.Row.Cells[9].Visible = false;
                    e.Row.Cells[10].Visible = false;
                }
            
                //string jsstrIP = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
                //if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
                //{
                //    if (jsstrIP == System.Configuration.ConfigurationManager.AppSettings["FCSERVER"])
                //    {
                //        GridList.Columns[9].Visible = false;
                //    }
                //    else
                //    {
                //        if (!_EluZSDataProvider.IsSZSB(userid))
                //        {
                //            GridList.Columns[10].Visible = false;
                //        }
                //        //else
                //        //{
                //        //    if (!_EluZSDataProvider.IsSQLXPro(strPID))
                //        //    {
                //        //        btn1.Visible = true;
                //        //    }
                //        //    else
                //        //    {
                //        //        btn1.Visible = true;
                //        //    }
                //        //}
                //    }
                //}
            }
        }
        /// <summary>
        /// 修改提交状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            int tt = _EluZSDataProvider.GetProjectFJNo(pID,"","1");
            if (tt < 2)
            {
                HttpContext.Current.Response.Write("<script>alert('请上传项目批准文件、规划许可文件附件后再提交！');window.location.href=window.location.href;</script>");
            }
            else
            {
                bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "提交状态", pID, "1");
                _EluZSDataProvider.UpdateCanSync("zs_file", "itemid", pID, " and use_type=1");
                _EluZSDataProvider.InsertZSFW(pID, userid, "0", "立项申请");
            }
            Bind();
        }

        /// <summary>
        /// 修改审核通过状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatusSh(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            //bool change = false;

            //if (btn.Text == "初审")
            //{
            //    change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "审批状态", pID, "-2");
            //}
            //if (btn.Text == "复审")
            //{
            //    change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "审批状态", pID, "-1");
            //}
            //if (btn.Text == "核审")
            //{
            //    change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "审批状态", pID, "1");
            //}
            //if (change) { Bind(); }

            string strScript = string.Format("<script>OpenWin('{0}')</script>",pID);
            this.Page.ClientScript.RegisterStartupScript(this.GetType(),"regLxsqYj", strScript);
        }

        /// <summary>
        /// 修改审核通过状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus1(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "审批状态", pID, "1");
            if (change) { Bind(); }
        }
        /// <summary>
        /// 撤销提交状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT","提交状态", pID, "0");
            if (change) { Bind(); }
        }
        /// <summary>
        /// 提醒开户状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangeStatus3(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool change = _EluZSDataProvider.GetProjectZT("ZS_PROJECT", "提醒开户状态", pID, "1");
            if (change) { Bind(); }
        }
        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Delete_Xm_Click(object sender, EventArgs e)
        {
            CustomGridView GridList = (CustomGridView)FindControl("GridList");
            LinkButton lBtn = sender as LinkButton;
            GridViewRow gvr = lBtn.Parent.Parent as GridViewRow;
            string ID = GridList.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_project", "ID", ID);
            if (del)
            {
                _EluZSDataProvider.DeleteXXByID(ID);
                HttpContext.Current.Response.Redirect("ProjectList.aspx");
            }
        }
        protected string GetSqlWhere() {
            TextBox xmmc = (TextBox)FindControl("xmmc");
            TextBox lxh = (TextBox)FindControl("lxh");
            TextBox jsdw = (TextBox)FindControl("jsdw");
            TextBox zsssdw = (TextBox)FindControl("zsssdw");
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(xmmc.Text.Trim()))
            {
                if (xmmc.Text == "项目总分类")
                {
                    HttpContext.Current.Response.Write("<script>javascript:window.parent.open('FileShow.aspx?zfl=1');</script>");
                }
                else
                {
                    sql += " and p.项目名称  like '%" + xmmc.Text.Trim() + "%'";
                }
            }
            if (!string.IsNullOrEmpty(lxh.Text.Trim()))
            {
                sql += " and p.立项号  like '%" + lxh.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(jsdw.Text.Trim()))
            {
                sql += " and p.建设单位  like '%" + jsdw.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(zsssdw.Text.Trim()))
            {
                sql += " and p.征收实施单位  like '%" + zsssdw.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["TYPE"]))
            {
                sql += " and p.区属 like '%" + HttpContext.Current.Request["TYPE"] + "%'";
                sql += " and p.id in (select pid from zs_prono where 审核状态=1)";
            }
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["typeqs"]))
            {
                sql += string.Format(@" and p.id in (select pnid from 
ZS_BCBASICINFO t where 开户银行 like '%{0}%' and pnid in(select id from zs_project))", HttpContext.Current.Request["typeqs"]);
            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sql += @" And p.区属 Like '%'|| substr(rolename,1,2) ||'%'
   And r.id In(Select roleid From zs_user_in_role Where userid Like '%"+userid+"%')";
            }
            sql += " order by p.addtime desc";
            return sql;
        }


        public  List<Project> GetProject(string sqlwhere)
        {
            List<Project> menulist = new List<Project>();
            string sql = "select * from zs_project p, zs_role r where 1=1";
            if (!string.IsNullOrEmpty(sqlwhere)) { sql += sqlwhere; }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return menulist = DataHelper.DataTableToT<Project>(dt);
        }

        public  List<Project> GetProjects(string sqlwhere)
        {
            List<Project> menulist = new List<Project>();
            string sql = "select * from zs_project p where 1=1";
            if (!string.IsNullOrEmpty(sqlwhere)) { sql += sqlwhere; }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return menulist = DataHelper.DataTableToT<Project>(dt);
        }
    }
}


